package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileExternalUnitsForm;
import com.ruoyi.business.mapper.FileExternalUnitsFormMapper;
import com.ruoyi.business.service.IFileExternalUnitsFormService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateExtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 外单位转入Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class FileExternalUnitsFormServiceImpl implements IFileExternalUnitsFormService
{
    @Autowired
    private FileExternalUnitsFormMapper fileExternalUnitsFormMapper;

    /**
     * 查询外单位转入
     *
     * @param id 外单位转入ID
     * @return 外单位转入
     */
    @Override
    public FileExternalUnitsForm selectFileExternalUnitsFormById(Long id)
    {
        return fileExternalUnitsFormMapper.selectFileExternalUnitsFormById(id);
    }

    /**
     * 查询外单位转入列表
     *
     * @param fileExternalUnitsForm 外单位转入
     * @return 外单位转入
     */
    @Override
    public List<FileExternalUnitsForm> selectFileExternalUnitsFormList(FileExternalUnitsForm fileExternalUnitsForm)
    {
        return fileExternalUnitsFormMapper.selectFileExternalUnitsFormList(fileExternalUnitsForm);
    }

    @Override
    public TableDataInfo<FileExternalUnitsForm> selectPageFileExternalUnitsFormList(FileExternalUnitsForm fileExternalUnitsForm, PageQuery pageQuery) {
        Page<FileExternalUnitsForm> FileExternalUnitsFormPage = fileExternalUnitsFormMapper.selectJoinPage(pageQuery.build(), FileExternalUnitsForm.class, this.buildQueryWrapper(fileExternalUnitsForm));
        return TableDataInfo.build(FileExternalUnitsFormPage);
    }

    private MPJLambdaWrapper<FileExternalUnitsForm> buildQueryWrapper(FileExternalUnitsForm fileExternalUnitsForm) {
        Map<String, Object> params = fileExternalUnitsForm.getParams();
        MPJLambdaWrapper<FileExternalUnitsForm> fileExternalUnitsFormJoinLambdaWrapper = new MPJLambdaWrapper<FileExternalUnitsForm>();
        fileExternalUnitsFormJoinLambdaWrapper.selectAll(FileExternalUnitsForm.class).selectAs(SysUser::getNickName,FileExternalUnitsForm::getRecipientsName)
            .leftJoin(SysUser.class,SysUser::getUserId,FileExternalUnitsForm :: getRecipients)
            .like(StrUtil.isNotBlank(fileExternalUnitsForm.getComingUnits()),FileExternalUnitsForm :: getComingUnits,fileExternalUnitsForm.getComingUnits())
            .eq(BeanUtil.isNotEmpty(fileExternalUnitsForm.getTransmittingType()),FileExternalUnitsForm :: getTransmittingType,fileExternalUnitsForm.getTransmittingType())
            .between(params.containsKey("beginTransmittingDate"),FileExternalUnitsForm :: getTransmittingDate,params.get("beginTransmittingDate"),params.get("endTransmittingDate"))
            .like(StrUtil.isNotBlank(fileExternalUnitsForm.getDeliveredPeople()),FileExternalUnitsForm :: getDeliveredPeople,fileExternalUnitsForm.getDeliveredPeople())
            .eq(BeanUtil.isNotEmpty(fileExternalUnitsForm.getRecipients()),FileExternalUnitsForm :: getRecipients,fileExternalUnitsForm.getRecipients());

        return fileExternalUnitsFormJoinLambdaWrapper;
    }

    /**
     * 新增外单位转入
     *
     * @param fileExternalUnitsForm 外单位转入
     * @return 结果
     */
    @Override
    public int insertFileExternalUnitsForm(FileExternalUnitsForm fileExternalUnitsForm)
    {
        fileExternalUnitsForm.setCreateTime(DateExtUtils.getNowDate());
        return fileExternalUnitsFormMapper.insertFileExternalUnitsForm(fileExternalUnitsForm);
    }

    /**
     * 修改外单位转入
     *
     * @param fileExternalUnitsForm 外单位转入
     * @return 结果
     */
    @Override
    public int updateFileExternalUnitsForm(FileExternalUnitsForm fileExternalUnitsForm)
    {
        fileExternalUnitsForm.setUpdateTime(DateExtUtils.getNowDate());
        return fileExternalUnitsFormMapper.updateFileExternalUnitsForm(fileExternalUnitsForm);
    }

    /**
     * 批量删除外单位转入
     *
     * @param ids 需要删除的外单位转入ID
     * @return 结果
     */
    @Override
    public int deleteFileExternalUnitsFormByIds(Long[] ids)
    {
        return fileExternalUnitsFormMapper.deleteFileExternalUnitsFormByIds(ids);
    }

    /**
     * 删除外单位转入信息
     *
     * @param id 外单位转入ID
     * @return 结果
     */
    @Override
    public int deleteFileExternalUnitsFormById(Long id)
    {
        return fileExternalUnitsFormMapper.deleteFileExternalUnitsFormById(id);
    }
}
